package com.androturka.billintur.cyberpark;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TimePicker;
import com.androturka.cyberpark.R;

public class SettingsActivity extends Activity{
	
	YemekhaneDB DB;
	String timeStr = "0000";
	TimePicker timePicker;
	CheckBox cbAutoNotification;
	boolean workAtBackGroundDB = true;
	int notificationTimeDB;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        
        timePicker = ( TimePicker )findViewById(R.id.timePicker1);
        timePicker.setIs24HourView(true);
        cbAutoNotification = ( CheckBox ) findViewById(R.id.autoNotification);
        
        
        DB = YemekhaneDB.getInstance(getApplicationContext());
    	String notificationTime = DB.getNotificationTime();
    	workAtBackGroundDB = DB.getWorkAtBackGround();
    	Log.d(Common.TAG,"notification time : " + notificationTime + " lenght : " + notificationTime.length());
    	
    	
    	notificationTimeDB = Integer.parseInt(notificationTime);
    	timeStr = notificationTime;
    	
    	int hourDB = notificationTimeDB / 100;
    	int minuteDB = notificationTimeDB % 100;
    	

    	Log.d(Common.TAG, hourDB + " - " + minuteDB );
    	timePicker.setCurrentHour(hourDB);
    	timePicker.setCurrentMinute(minuteDB);
		
		
		cbAutoNotification.setChecked(workAtBackGroundDB);
		timePicker.setEnabled(workAtBackGroundDB);
		//TODO her değişiklikte unu yapmak sistemi çok yorabilir bunu yapmak yerine sayfadan çıkıldığında kaydetme işlemi yapılabilir.
        TimePicker.OnTimeChangedListener mTimerChangedListener =
            new TimePicker.OnTimeChangedListener() {

            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                timeStr = String.format("%02d", hourOfDay)+String.format("%02d", minute);
                
            }
        };

        timePicker.setOnTimeChangedListener(mTimerChangedListener);
        
    	cbAutoNotification.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    		 
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				timePicker.setEnabled(isChecked);
				}
    		});
    }
    
    public void onPause() {
    	super.onPause();
    	
    	
    	Log.d(Common.TAG, "onPause " + timeStr);
        DB.updateNotificationTime(timeStr);
        int timeInt = Integer.parseInt(timeStr);
        
		MenuDownloader downloader = new MenuDownloader(this);		

        if ( cbAutoNotification.isChecked()){	
        	DB.updateWorkAtBackGround(true);
        	if ( workAtBackGroundDB == false )
        	{
        		Log.d(Common.TAG,"otomatik uyari kapaliymis ama su an acik Alarmi kur");
        		//TODO otomatik uyari kapaliymis ama su an acik
        		//Alarmi kur

        		downloader.setAlarm();
        	}
        	else if ( timeInt != notificationTimeDB ){
        		//TODO eski alarmı iptal et yenisini kur
        		Log.d(Common.TAG,"eski alarmı iptal et yenisini kur");
        		downloader.setAlarm();
        	}        		
        }
        		
        else {	
        	DB.updateWorkAtBackGround(false);
        	
        	if ( workAtBackGroundDB ){
        		//TODO otomatik arama acikmis su an kapali
        		//alarmi iptal et
        		
        		downloader.cancelAlarm();
        		Log.d(Common.TAG,"otomatik arama acikmis su an kapali alarmi iptal et");
        	}
        	else{
        		Log.d(Common.TAG,"Zaten önceden de kapaliymis");
        	}
        }
	}
}
